import dayjs from 'dayjs'
import isToday from 'dayjs/plugin/isToday'
import isYesterday from 'dayjs/plugin/isYesterday'

dayjs.extend(isToday)
dayjs.extend(isYesterday)

/**
 * 延迟执行
 * @param delay number 延迟时间
 * @returns Promise
 */
export function sleep(delay: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, delay)
  })
}

/**
 * 格式化时间
 * @param {Date|string|number} date - 需要格式化的日期
 * @returns {string} 格式化后的时间字符串
 */
export function formatTime(date: string | number) {
  const d = dayjs(date)

  if (d.isToday()) {
    return `今天 ${d.format('HH:mm')}`
  } else if (d.isYesterday()) {
    return `昨天 ${d.format('HH:mm')}`
  } else {
    return d.format('YYYY-MM-DD HH:mm')
  }
}
